/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hoabui
 */
public class frmQLBaiThi extends javax.swing.JFrame {

    /**
     * Creates new form frmQLBaiThi
     */
    public frmQLBaiThi() {
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lblDeThi = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taDeBai = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        tfCau1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tfCau2 = new javax.swing.JTextField();
        tfCau4 = new javax.swing.JTextField();
        tfCau3 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblDSBaiThi = new javax.swing.JTable();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnCapNhat = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        cmbDapAn = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Đề thi : ");

        lblDeThi.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblDeThi.setText("...");

        jLabel2.setText("Đề bài");

        taDeBai.setColumns(20);
        taDeBai.setRows(5);
        jScrollPane1.setViewportView(taDeBai);

        jLabel3.setText("Câu hỏi 1");

        jLabel4.setText("Câu hỏi 3");

        jLabel5.setText("Câu hỏi 2");

        jLabel6.setText("Câu hỏi 4");

        tblDSBaiThi.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDSBaiThiMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblDSBaiThi);

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setText("Xoá");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnCapNhat.setText("Cập nhật");
        btnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapNhatActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jLabel7.setText("Đáp án đúng");

        cmbDapAn.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--- Chọn đáp án ----", "Câu 1", "Câu 2", "Câu 3", "Câu 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(116, 116, 116)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblDeThi))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnThem)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnXoa)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCapNhat)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnThoat))
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3)
                                            .addComponent(tfCau1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel4))
                                        .addGap(22, 22, 22))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel7)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(cmbDapAn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(tfCau3, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel6)
                                    .addComponent(tfCau2, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                                    .addComponent(jLabel5)
                                    .addComponent(tfCau4))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblDeThi))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tfCau1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCau2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfCau4, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCau3, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(cmbDapAn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnCapNhat)
                    .addComponent(btnThoat))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //Bien luu de thi tuong ung cua cac bai thi
    private DeThi deThi;
    // Danh sach bai thi
    private List<BaiThi> dsBaiThi = new ArrayList<BaiThi>();

    //Ham get de thi
    public DeThi getDeThi() {
        return deThi;
    }
//Ham set de thi
    public void setDeThi(DeThi deThi) {
        this.deThi = deThi;
        lblDeThi.setText(this.deThi.getTieuDe());

    }

    //Load danh sach bai thi len table
    private void loadDSBaiThi() {
        dsBaiThi = BaiThi.layBaiThiTheoMaDe(this.deThi.getMaDe());
        //Tao model de load len jtable
        DefaultTableModel model = new DefaultTableModel();
        //Them cot cho model
        model.addColumn("Mã bài thi");
        model.addColumn("Đề bài");
        model.addColumn("Đáp án 1");
        model.addColumn("Đáp án 2");
        model.addColumn("Đáp án 3");
        model.addColumn("Đáp án 4");
        model.addColumn("Đáp án đúng");

        //Them dong cho model
        for (int i = 0; i < dsBaiThi.size(); i++) {
            BaiThi bt = dsBaiThi.get(i);
            Vector v = new Vector();
            v.add(bt.getMaBaiThi());
            v.add(bt.getDeBai());
            v.add(bt.getDapAn1());
            v.add(bt.getDapAn2());
            v.add(bt.getDapAn3());
            v.add(bt.getDapAn4());
            v.add(bt.getDapAnDung());
            model.addRow(v);

        }
        //set model cho table
        tblDSBaiThi.setModel(model);
    }

    //Ham kiem tra du lieu nhap
    private boolean kiemTraDuLieu() {
        //Neu chua nhap de bai thi bao loi
        if (taDeBai.getText().length() < 1) {
            JOptionPane.showMessageDialog(null, "Vui lòng nhập đề bài", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            return false;
        }

        //Neu chua nhap cac cau hoi thi bao loi
        if (tfCau1.getText().length() < 1 || tfCau2.getText().length() < 1 || tfCau3.getText().length() < 1 || tfCau4.getText().length() < 1) {
            JOptionPane.showMessageDialog(null, "Vui lòng nhập câu hỏi", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            return false;
        }

        //Neu chua chon dap an dung thi bao loi
        int index = cmbDapAn.getSelectedIndex();
        if (index < 1) {
            JOptionPane.showMessageDialog(null, "Vui lòng chọn đáp án đúng", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            return false;
        }
        return true;
    }
    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        //Load danh sach bai thi khi mo form
        loadDSBaiThi();
    }//GEN-LAST:event_formWindowOpened

    //Su kien bam nut them
    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        
        //Kiem tra du lieu thanh cong se tien hanh them
        if (kiemTraDuLieu()) {
            //Tao bai thi dua vao du lieu tren form
            BaiThi bt = new BaiThi();
            bt.setMaDeThi(this.deThi.getMaDe());
            bt.setDeBai(taDeBai.getText());
            bt.setDapAn1(tfCau1.getText());
            bt.setDapAn2(tfCau2.getText());
            bt.setDapAn3(tfCau3.getText());
            bt.setDapAn4(tfCau4.getText());
            bt.setDapAnDung(cmbDapAn.getSelectedIndex());
            //Goi ham them bai thi
            if (bt.ThemBaiThi()) {
                JOptionPane.showMessageDialog(null, "Thêm bài thi thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                loadDSBaiThi();
            } else {
                JOptionPane.showMessageDialog(null, "Thêm bài thi thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnThemActionPerformed

    //Su kien bam vao 1 dong tren jtable
    private void tblDSBaiThiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDSBaiThiMouseClicked
        // TODO add your handling code here:
        //Lay vi tri cua dong duoc chon
        int index = tblDSBaiThi.getSelectedRow();
        //Neu vi tri >=0 thi lay ra bai thi tai vi tri hien thi len form
        if (index >= 0) {
            BaiThi bt = dsBaiThi.get(index);
            taDeBai.setText(bt.getDeBai());
            tfCau1.setText(bt.getDapAn1());
            tfCau2.setText(bt.getDapAn2());
            tfCau3.setText(bt.getDapAn3());
            tfCau4.setText(bt.getDapAn4());
            cmbDapAn.setSelectedIndex(bt.getDapAnDung());
        }
    }//GEN-LAST:event_tblDSBaiThiMouseClicked

    //Su kien nut cap nhat
    private void btnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapNhatActionPerformed
        // TODO add your handling code here:
        //Lay vi tri cua dong duoc chon tren jtable
        int index = tblDSBaiThi.getSelectedRow();
        //Neu vi tri >= 0 thi tien hanh cap nhat
        if (index >= 0) {
            //Cap nhat du lieu cho bai thi
            BaiThi bt = dsBaiThi.get(index);
            if (kiemTraDuLieu()) {
                bt = new BaiThi();
                bt.setMaDeThi(this.deThi.getMaDe());
                bt.setDeBai(taDeBai.getText());
                bt.setDapAn1(tfCau1.getText());
                bt.setDapAn2(tfCau2.getText());
                bt.setDapAn3(tfCau3.getText());
                bt.setDapAn4(tfCau4.getText());
                bt.setDapAnDung(cmbDapAn.getSelectedIndex());
                if (bt.CapNhatBaiThi()) {
                    JOptionPane.showMessageDialog(null, "Cập nhật bài thi thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                    loadDSBaiThi();
                } else {
                    JOptionPane.showMessageDialog(null, "Cập nhật bài thi thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Vui lòng chọn bài thi", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }
    }//GEN-LAST:event_btnCapNhatActionPerformed

    //Su kien nut xoa
    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
         //Lay vi tri cua dong duoc chon tren jtable
        int index = tblDSBaiThi.getSelectedRow();
         //Neu vi tri >= 0 thi tien hanh xoa
        if (index >= 0) {
            BaiThi bt = dsBaiThi.get(index);

            if (BaiThi.XoaBaiThi(bt.getMaBaiThi())) {
                JOptionPane.showMessageDialog(null, "Xoá bài thi thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                loadDSBaiThi();
            } else {
                JOptionPane.showMessageDialog(null, "Xoá bài thi thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Vui lòng chọn bài thi", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }

    }//GEN-LAST:event_btnXoaActionPerformed

    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cmbDapAn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblDeThi;
    private javax.swing.JTextArea taDeBai;
    private javax.swing.JTable tblDSBaiThi;
    private javax.swing.JTextField tfCau1;
    private javax.swing.JTextField tfCau2;
    private javax.swing.JTextField tfCau3;
    private javax.swing.JTextField tfCau4;
    // End of variables declaration//GEN-END:variables
}
